package com.wushi.lenovo.asia5b.app_ui.inbox.adapter;

import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.common.GlobalValue;
import com.mvp.images.WImageLoader;
import com.mvp.utils.ResString;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_event.ReadNotificationEvent;
import com.wushi.lenovo.asia5b.app_model.inbox.InboxMsgModel;
import com.wushi.lenovo.asia5b.app_model.inbox.InboxMsgType;
import com.wushi.lenovo.asia5b.app_utils.helper.UriHelper;
import com.wushi.lenovo.asia5b.app_widget.span.InboxLink;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import timber.log.Timber;

/**
 * Created by zhangyuncai on 2019/1/7.
 */
public class InboxMsgAdapter extends BaseQuickAdapter<InboxMsgModel, BaseViewHolder> {
    private final InboxMsgType inboxMsgType;

    public InboxMsgAdapter(@Nullable List<InboxMsgModel> data, InboxMsgType inboxMsgType) {
        super(R.layout.item_inboxmsg_adapter, data);
        this.inboxMsgType = inboxMsgType;

    }

    @Override
    protected void convert(BaseViewHolder helper, InboxMsgModel item) {

        TextView tv_inbox_googdetail_title = helper.getView(R.id.tv_inbox_googdetail_title);
        TextView tv_googdetail_goodname = helper.getView(R.id.tv_googdetail_goodname);
        TextView tv_googdetail_size = helper.getView(R.id.tv_googdetail_size);
        TextView tv_googdetail_quantity = helper.getView(R.id.tv_googdetail_quantity);
        ImageView iv_googdetail_img = helper.getView(R.id.iv_googdetail_img);
        LinearLayout ll_inbox_googdetail = helper.getView(R.id.ll_inbox_googdetail);

        InboxMsgModel.ProductInfo extra_msg = item.getExtra_msg();
        if (extra_msg != null) {
            ll_inbox_googdetail.setVisibility(View.VISIBLE);
            String title = extra_msg.getTitle();
            tv_inbox_googdetail_title.setText(title);
            InboxMsgModel.ProductInfoBean product_info = extra_msg.getProduct_info();
            if (product_info != null) {
                //商品名称
                tv_googdetail_goodname.setText(product_info.getName());
                if (!TextUtils.isEmpty(product_info.getLink())) {
                    tv_googdetail_goodname.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark));
                    tv_googdetail_goodname.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
                    tv_googdetail_goodname.getPaint().setAntiAlias(true);//抗锯齿z
                    tv_googdetail_goodname.setOnClickListener(v -> {
                        if (!item.getCheck_read()) {
                            Timber.d("--->>>>>>>>---1/id:" + item.getId());
                            EventBus.getDefault().post(new ReadNotificationEvent(item.getId(), inboxMsgType == InboxMsgType.inbox ? "promo_notification" : "order_notification"));
                        }
                        UriHelper.parseUri(product_info.getLink(), mContext);
                    });
                } else {
                    tv_googdetail_goodname.setTextColor(ContextCompat.getColor(mContext, R.color.common_text_black));
                    tv_googdetail_goodname.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                    tv_googdetail_goodname.getPaint().setAntiAlias(true);
                }
                //商品图片
                WImageLoader.displayImage(mContext, product_info.getImage(), iv_googdetail_img);
                //属性
                tv_googdetail_size.setText(String.format(ResString.get(R.string.orderitem_details), product_info.getOption()));
                //数量
                tv_googdetail_quantity.setText(String.format(ResString.get(R.string.orderitem_quantity), product_info.getQuantity()));
            }
        } else {
            ll_inbox_googdetail.setVisibility(View.GONE);
        }

        int position = helper.getLayoutPosition();

        //标题
        helper.setText(R.id.tv_inbox_title, item.getTitle() + (GlobalValue.isDebug ? item.getId() : ""));
        TextView tv_inbox_title = helper.getView(R.id.tv_inbox_title);
        TextPaint paint = tv_inbox_title.getPaint();
        paint.setFakeBoldText(!item.getCheck_read());

        //图片
        ImageView iv_inbox_img = helper.getView(R.id.iv_inbox_img);
        iv_inbox_img.setVisibility(View.GONE);
        if (inboxMsgType == InboxMsgType.inbox || !TextUtils.isEmpty(item.getMobile_image())) {
            iv_inbox_img.setVisibility(View.VISIBLE);
            WImageLoader.displayImage(mContext, item.getMobile_image(), iv_inbox_img);
        }


        //内容
        Spanned content = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            content = Html.fromHtml(item.getMessage(), Html.FROM_HTML_MODE_COMPACT);
        } else {
            content = Html.fromHtml(item.getMessage());
        }
        TextView tv_inbox_content = helper.getView(R.id.tv_inbox_content);
        tv_inbox_content.setText(content);
        tv_inbox_content.setMovementMethod(InboxLink.getInstance(item));

        //时间
        helper.setText(R.id.tv_inbox_time, item.getCreated_at());

        //分割线 view_voucher_topline  view_voucher_bottomline
        helper.setGone(R.id.view_inbox_bottomline, position == (mData.size() - 1));


    }

}
